package com.guigu.health.service.impl;

import com.guigu.health.dao.MenuMapper;
import com.guigu.health.pojo.Menu;
import com.guigu.health.pojo.MenuParentTree;
import com.guigu.health.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName:MenuServiceImpl
 * @Description:
 * @Author:longtao
 * @Date:2021/4/9
 * @Version:1.0
 */
@Service("menuService")
public class MenuServiceImpl implements MenuService {

    private MenuMapper menuMapper;
    @Autowired
    @Qualifier("menuMapper")
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    @Override
    public List<Menu> queryAll(String account) {
        List<Menu> list = menuMapper.queryAll(account);
        return list;
    }

    /**
     * 根据角色ID获取它们的id
     *
     * @param roleID
     * @return
     */
    @Override
    public List<Integer> findMenuChildByMenuParent(Integer roleID) {
        List<Integer> list = menuMapper.findMenuChildByMenuParent(roleID);
        return list;
    }

    /**
     * 获取菜单集
     *
     * @return
     */
    @Override
    public List<MenuParentTree> queryAllParent() {
        List<MenuParentTree> trees = menuMapper.queryAllMenu();
        return trees;
    }

    /**
     * 修改菜单项
     *
     * @param menuIds
     */

    @Override
    @Transactional
    public void updateMenu(Integer roleID,Integer[] menuIds) {
        /**
         * 删除旧关系
         */
        menuMapper.deleteMenu(roleID);
        /**
         * 建立新关系
         */
        if (menuIds!=null && menuIds.length>0){
            for (Integer id : menuIds) {
                menuMapper.addRoleMenu(roleID,id);
            }
        }
    }
}
